package ru.spb.nicetu.altx.mnemo.node;

import java.io.Serializable;

import ru.spb.nicetu.altx.mnemo.cell.INodeCell;
import ru.spb.nicetu.altx.mnemo.math.managers.NodeManager;

@SuppressWarnings("serial")
public class CellLink implements ICellLink, Serializable {

  transient private INode node;
  private long nodeID;
  private INodeCell cell;

  public CellLink(INode node, INodeCell cell) {
    super();
    this.cell = cell;
    this.node = node;
    this.nodeID = node.getID();
  }

  
  public INodeCell getCell() {
    return cell;
  }
  
  public void setCell(INodeCell cell) {
	  this.cell = cell;
	}

  
  public INode getNode() {
    // TODO !!! Срочно доработать CellLink.getNode
    if (this.node != null)
      return this.node;
    if (this.nodeID != 0) {
      INode n = NodeManager.getInstance().getNodeByID(nodeID);
      if (n != null) {
        this.node = n;
        return this.node;
      }
    }
    return null;
  }
  
  public void setNode(INode node) {
	  this.node = node;
	  this.nodeID = this.node.getID();
	}

  
  public long getNodeID() {
    return nodeID;
  }
  
  public void setNodeID(long nodeID) {
	  this.nodeID = nodeID;
	}

  
  public boolean equals(Object o) {
    if (o == null)
      return false;
    if (getClass() != o.getClass())
      return false;

    ICellLink other = (ICellLink) o;

    if ((null == cell) || (0 == nodeID))
      return false;

    return (nodeID == other.getNodeID()) && (cell.equals(other.getCell()));
  }

  
  public int hashCode() {

    return getNode().hashCode() + 137 * cell.hashCode();
  }

  
  public int compareTo(ICellLink arg0) {
    if (this.hashCode() > arg0.hashCode())
      return 1;
    else if (this.hashCode() < arg0.hashCode())
      return -1;
    else
      return 0;
  }

}
